// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
//
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.ui.web;

import java.util.List;

import org.dengues.commons.DenguesCommonsPlugin;
import org.dengues.commons.ui.swt.colorstyledtext.ColorManager;
import org.dengues.commons.ui.swt.colorstyledtext.ColorStyledText;
import org.dengues.commons.ui.swt.colorstyledtext.EModeLanguage;
import org.dengues.ui.web.browser.dom.HTMLDocument;
import org.dengues.ui.web.browser.dom.HTMLElement;
import org.dengues.ui.web.browser.dom.Node;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;

/**
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf 2007-12-5 qiang.zhang $
 * 
 */
public class HTMLStructureComposite extends Composite {

    private TreeViewer treeViewer;

    private ColorStyledText code;

    private String tdText;

    /**
     * Qiang.Zhang.Adolf@gmail.com HTMLStructureComposite constructor comment.
     * 
     * @param parent
     */
    public HTMLStructureComposite(Composite parent) {
        super(parent, SWT.BORDER);
        setBackground(parent.getBackground());
        GridLayout gridLayout = new GridLayout();
        gridLayout.marginBottom = 0;
        gridLayout.marginWidth = 0;
        gridLayout.marginTop = 0;
        gridLayout.marginRight = 0;
        gridLayout.marginLeft = 0;
        gridLayout.marginHeight = 0;

        setLayout(gridLayout);

        GridData gridData = new GridData(GridData.FILL_BOTH);
        setLayoutData(gridData);
        SashForm mainForm = new SashForm(this, SWT.VERTICAL);
        mainForm.setLayout(new GridLayout());
        mainForm.setLayoutData(new GridData(GridData.FILL_BOTH));
        createNodeStructure(mainForm);
        createCodeViewer(mainForm);
        mainForm.setWeights(new int[] { 3, 1 });
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createCodeViewer".
     * 
     * @param mainForm
     */
    private void createCodeViewer(SashForm mainForm) {
        ColorManager colorManager = new ColorManager(DenguesCommonsPlugin.getDefault().getPreferenceStore());
        code = new ColorStyledText(mainForm, SWT.H_SCROLL | SWT.V_SCROLL, colorManager, EModeLanguage.HTML.getLanguageMode());
        Font font = new Font(this.getDisplay(), "courier", 10, SWT.NONE); //$NON-NLS-1$
        code.setFont(font);
        GridData gd = new GridData(GridData.FILL_BOTH);
        code.setLayoutData(gd);
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "createNodeStructure".
     * 
     * @param mainForm
     */
    private void createNodeStructure(SashForm mainForm) {
        treeViewer = new TreeViewer(mainForm, SWT.NONE);
        HTMLStructureViewerProvider provider = new HTMLStructureViewerProvider();
        treeViewer.setContentProvider(provider);
        treeViewer.setLabelProvider(provider);
        treeViewer.addSelectionChangedListener(new ISelectionChangedListener() {

            public void selectionChanged(SelectionChangedEvent event) {
                IStructuredSelection selection = (IStructuredSelection) event.getSelection();
                List list = selection.toList();
                if (list.size() != 1) {
                    code.setText(""); //$NON-NLS-1$
                    return;
                }
                final Node node = (Node) list.get(0);
                try {
                    Display.getDefault().asyncExec(new Runnable() {

                        public void run() {
                            String textContent = ""; //$NON-NLS-1$
                            if (node instanceof HTMLElement) {
                                textContent = ((HTMLElement) node).getOuterHTML();
                                tdText = ((HTMLElement) node).getInnerText();
                            }
                            if (textContent != null) {
                                code.setText(textContent);
                            }
                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Qiang.Zhang.Adolf@gmail.com Comment method "setInput".
     * 
     * @param document
     */
    public void setInput(HTMLDocument document) {
        treeViewer.setInput(document);
    }

    /**
     * Getter for tdText.
     * 
     * @return the tdText
     */
    public String getTdText() {
        return this.tdText;
    }
}
